package cn.sinata.wanglin.publish.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.view.SwipeRefreshRecyclerLayout;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnTextChanged;
import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.AddressAdapter;
import cn.sinata.wanglin.neighbor.view.SearchEditText;
/**
 * 搜索地址
 */
public class SreachActivity extends BaseActivity {
    @BindView(R.id.mSwipeRefreshLayout)
    SwipeRefreshRecyclerLayout mSwipeRefreshLayout;
    @BindView(R.id.tv_search)
    SearchEditText tv_search;
    private ArrayList<PoiInfo> mAddressinfos = new ArrayList<>();
    private AddressAdapter mAddressAdapter;
    private PoiSearch poiSearch=null;
    private int pageNo = 0;
    private int pageSize = 20;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sreach);
        ButterKnife.bind(this);
        initView();
        initBaiduSearch();
    }

    private void initBaiduSearch() {
        // 实例化PoiSearch对象
        poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(poiListener);

    }
    private  void searchAddress(){
        String keyword= tv_search.getText().toString().trim();
        if(TextUtils.isEmpty(keyword)){
            mSwipeRefreshLayout.setRefreshing(false);
            showToast("请先输入关键字！");
            return;
        }
        PoiCitySearchOption citySearchOption = new PoiCitySearchOption();
        citySearchOption.city(MyApplication.city);// 城市
        citySearchOption.keyword(keyword);// 关键字
        citySearchOption.pageCapacity(pageSize);// 默认每页10条
        citySearchOption.pageNum(pageNo);// 分页编号
        // 发起检索请求
        poiSearch.searchInCity(citySearchOption);
    }
    OnGetPoiSearchResultListener poiListener=new OnGetPoiSearchResultListener() {

        @Override
        public void onGetPoiResult(PoiResult result) {
            mSwipeRefreshLayout.setRefreshing(false);
            if (pageNo == 1) {
                mAddressinfos.clear();
            }
            if(null==result.getAllPoi()){
                if (pageNo == 1) {
                    showToast("暂无数据");
                } else {
                    pageNo --;
                    showToast("没有更多了");
                }
            }else{
                mAddressinfos.addAll(result.getAllPoi());
            }
            mAddressAdapter.notifyDataSetChanged();
        }
        @Override
        public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

        }
        @Override
        public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

        }

    };

    private void initView() {
        tv_search.setHint("请输入关键词");
        mAddressAdapter = new AddressAdapter(mAddressinfos);
        mSwipeRefreshLayout.setMode(SwipeRefreshRecyclerLayout.Mode.Both);
        mSwipeRefreshLayout.setLayoutManager(new LinearLayoutManager(mContext));
        mSwipeRefreshLayout.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .size(1)
                .color(ContextCompat.getColor(mContext, R.color.dividing_line_color))
                .build());
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshRecyclerLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                searchAddress();
            }

            @Override
            public void onLoadMore() {
                pageNo++;
                searchAddress();
            }
        });
        mSwipeRefreshLayout.setAdapter(mAddressAdapter);
        mAddressAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent mIntent=new Intent();
                mIntent.putExtra("name",mAddressinfos.get(position).name);
                mIntent.putExtra("address",mAddressinfos.get(position).address);
                mIntent.putExtra("longitude",""+mAddressinfos.get(position).location.longitude);
                mIntent.putExtra("latitude",""+mAddressinfos.get(position).location.latitude);
                setResult(RESULT_OK,mIntent);
                finish();
            }
        });
    }
    @OnTextChanged(value = R.id.tv_search,callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void textChang(Editable s){
        pageNo = 1;
        searchAddress();
    }
}
